;=========== LCD Define ==================================================================

;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------	
LCD_WriteNibble:
	sbi		LCD_CMD_PORT, LCD_EN

	sbrs	r17, LCD_DATA_BIT4
	cbi		LCD_PORT, LCD_DATA_BIT4
	sbrc	r17, LCD_DATA_BIT4
	sbi		LCD_PORT, LCD_DATA_BIT4
	
	sbrs	r17, LCD_DATA_BIT5
	cbi		LCD_PORT, LCD_DATA_BIT5
	sbrc	r17, LCD_DATA_BIT5
	sbi		LCD_PORT, LCD_DATA_BIT5
	
	sbrs	r17, LCD_DATA_BIT6
	cbi		LCD_PORT, LCD_DATA_BIT6
	sbrc	r17, LCD_DATA_BIT6
	sbi		LCD_PORT, LCD_DATA_BIT6
	
	sbrs	r17, LCD_DATA_BIT7
	cbi		LCD_PORT, LCD_DATA_BIT7
	sbrc	r17, LCD_DATA_BIT7
	sbi		LCD_PORT, LCD_DATA_BIT7
	
	cbi		LCD_CMD_PORT, LCD_EN
ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_WriteData:
	sbi		LCD_CMD_PORT, LCD_RS
	push	r17
	rcall	LCD_WriteNibble
	pop		r17
	swap	r17
	rcall	LCD_WriteNibble
	push XL
	push XH
	_Delay_4xCycles 250
	pop XH
	pop XL
ret


	StringToLCD:
		push r17
	StringToLCDLOOP:
		LD r17, X+
		CPI r17, 0
		breq StringToLCDExit
		rcall LCD_WriteData
		rjmp StringToLCDLOOP
	StringToLCDExit:
		pop r17
	ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_WriteCommand:
	cbi		LCD_CMD_PORT, LCD_RS
	push	r17
	swap	r17
	rcall	LCD_WriteNibble
	pop		r17
	rcall	LCD_WriteNibble

	_Delay_ms 2
	ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_WriteString:
	lpm		r17, Z+
	cpi		r17, 0
	breq	exit
	rcall	LCD_WriteData
	rjmp	LCD_WriteString
exit:
	ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_SetAddressDD:
	ori		r17, HD44780_DDRAM_SET
	rcall	LCD_WriteCommand
	ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_SetAddressCG:
	ori		r17, HD44780_CGRAM_SET
	rcall	LCD_WriteCommand
	ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_Init:
	sbi		LCD_DATA_DDR, LCD_D4
	sbi		LCD_DATA_DDR, LCD_D5
	sbi		LCD_DATA_DDR, LCD_D6
	sbi		LCD_DATA_DDR, LCD_D7
	
	sbi		LCD_CMD_DDR, LCD_RS
	sbi		LCD_CMD_DDR, LCD_EN
	sbi		LCD_CMD_DDR, LCD_RW

	cbi		LCD_CMD_PORT, LCD_RS
	cbi		LCD_CMD_PORT, LCD_EN
	cbi		LCD_CMD_PORT, LCD_RW

	_Delay_ms 100

	ldi		r16, 3
InitLoop:
	ldi		r17, 0x03
	rcall	LCD_WriteNibble

	push r16
	_Delay_ms 5
	pop r16

	dec		r16
	brne	InitLoop

	ldi		r17, 0x02
	rcall	LCD_WriteNibble

	_Delay_ms 1

	ldi		r17, HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_4_BIT
	rcall	LCD_WriteCommand

	ldi		r17, HD44780_DISPLAY_ONOFF | DISPLAY_OFF
	rcall	LCD_WriteCommand

	ldi		r17, HD44780_CLEAR
	rcall	LCD_WriteCommand

	ldi		r17, HD44780_ENTRY_MODE |HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT
	rcall	LCD_WriteCommand

	ldi		r17, HD44780_DISPLAY_ONOFF | DISPLAY_ON | CURSOR_OFF | CURSOR_NOBLINK
	rcall	LCD_WriteCommand

	ret

 ;------------------------------------------------------------------------------
 ; End of file
 ;------------------------------------------------------------------------------


